package com.ruimei.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruimei.entitys.Honor;
import com.ruimei.entitys.Parent;
import com.ruimei.service.HonorService;
import com.ruimei.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags={"学生荣誉"})
@Controller
public class HonorController {

    @Resource
    HonorService honorService;
    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @PostMapping({"/honor/add"})
    @ResponseBody
    @ApiOperation("家长端添加荣誉")
    public Map<String, Object> addHonor(Honor honor, HttpServletRequest request)
    {

        String  parentId = TokenUtils.getParentId(request);
        if (redisTemplate.hasKey(parentId)) {
            Parent parent = JSONObject.parseObject(redisTemplate.opsForValue().get(parentId), Parent.class);
            honor.setParentId(parentId);
            honor.setParentName(parent.getFullName());
        }
        Map<String, Object> map = honorService.addHonor(honor);
        return map;
    }

    @PostMapping({"/honor/getstudent"})
    @ResponseBody
    @ApiOperation("家长端获取荣誉通过学生ID")
    public Map<String, Object> getHonorByStudnetId(String studentId)
    {
        Map<String, Object> map = honorService.getHonorByStudnetId(studentId);
        return map;
    }

    @PostMapping({"/honor/getclass"})
    @ResponseBody
    @ApiOperation("教师端获取荣誉通过班级ID")
    public Map<String, Object> getHonorByClassId(String classId,String studentName)
    {
        Map<String, Object> map = honorService.getHonorByClassId(classId,studentName);
        return map;
    }

}
